package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    private Result findClazz ( ClazzQueryParam clazzQueryParam ) {

        PageResult pageResult = clazzService.findClazz(clazzQueryParam);

        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    private Result deleteClazz ( @PathVariable("id") Integer id ) {
        clazzService.deleteClazz(id);
        return Result.success();
    }

    @PostMapping
    private Result saveClazz ( @RequestBody Clazz clazz ) {
        clazzService.saveClazz(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClazzById ( @PathVariable("id") Integer id ) {
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }

    @GetMapping("/list")
    public Result findAllClazzs () {
        List<Clazz> clazzList = clazzService.findAllClazzs();
        return Result.success(clazzList);
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        clazzService.updateClazz(clazz);
        return Result.success();
    }


}
